﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Doo_JetDAL;
using System.Data.Objects.DataClasses;
using DoodJetView.Core.Reservation;
using System.Data.Objects;
using DoodJetView.VuesPartielles.Reservation;
using DoodJetView.VuesPartielles;

namespace DoodJetView.Core
{
    public class GestionReservation
    {
        public void CreerReservation(client client, EntityCollection<employes> employes, EntityCollection<equipements> equipements, int nbPersonnes, DateTime start, DateTime stop)
        {
            using (DataEntities db = new DataEntities())
            {
                var res = new reservation();
                res.client = client;
                res.employes = employes;
                res.equipements = equipements;
                res.NbDePersonnes = nbPersonnes;
                res.dateDebut = DateTimeUnixTools.ConvertToUnixTimestamp( start);
                res.dateFin = DateTimeUnixTools.ConvertToUnixTimestamp(stop);
                db.reservation.AddObject(res);
                db.SaveChanges();
            }
        }
        public void ModifierReservation(reservation reserToEdit)
        {
            
        }
        public void SupprimerReservation()
        {
        }
        public void RecupererReservation()
        {

        }
        public void RecupererReservations() 
        {
        
        }
        public static ReservationCreation reservationCreation { get; set; }
        public static DynamicGridTools verifieUneReservetion(int typeequipement, DateTime dt, ReservationCreation reservationCreation)
        {
            List<PartialDayOfActivities> ret = new List<PartialDayOfActivities>();
            DynamicGridTools data = new DynamicGridTools(650,false, reservationCreation);
            int i = 0;
            foreach (equipements item in GestionReservation.RecupererListEquipement(typeequipement))
            {
                data.CreateRow();
                PartialDayOfActivities res = new PartialDayOfActivities("Test de vue partielle", reservationCreation);
                //ret.RempliLagrille(ret,Duree, idClient, Activites, dt);
                res.DynamicGrid.Jours = dt;
                res.ReservationView = reservationCreation;
                res.Lbl_Titre.Content = item.nomEquipement;
                res.DynamicGrid.CurrentIDEquip = (int)item.id_equipement;
                res.DynamicGrid.Render();
                
                data.AddUiToCase(res, i, 0);
                i++;
            }
            return data;
         
        }
        public static List<equipements> RecupererListEquipement(int Activites)
        {
            List<equipements> produits = new List<equipements>();
            using (DataEntities db = new DataEntities())
            {
                foreach (equipements eq in db.equipements.Where(o => o.TypeEquipement.id_TypeEquipement == Activites).ToList())
                {
                    produits.Add(eq);
                }

                return produits;

            }

        }
        public static ObjectResult<EquipementResult> RecuperePlaningEquipement(DateTime dt, DataEntities db, equipements eq, int debut, int fin)
        {
            int dateDebut = (DateTimeUnixTools.ConvertToUnixTimestamp(dt) - DateTimeUnixTools.ConvertToUnixTimestamp((new DateTime()).AddHours(12)));
            int UnixFin = (DateTimeUnixTools.ConvertToUnixTimestamp(dt) + DateTimeUnixTools.ConvertToUnixTimestamp((new DateTime()).AddHours(12)));
            string nativeSQLQuery =
                "SELECT r.* , c.id_equipement, cl.nomClient, cl.prenomClient  FROM client cl, concerne c, reservation r, equipements e, TypeEquipement te" +
                "WHERE c.id_equipement = e.id_equipement" +
                "AND e.id_TypeEquipement = te.id_TypeEquipement" +
                "AND c.id_reservation = r.id_reservation" +
                "AND r.dateFin < " + fin +
                "AND r.dateDebut > " + debut +
                "AND e.id_equipement = " + eq.id_equipement +
                "GROUP By r.id_reservation";
            ObjectResult<EquipementResult> product = db.ExecuteStoreQuery<EquipementResult>(nativeSQLQuery);
            return product;
        }

        public static bool isReserved(int idEq, int debut)
        {
            DataEntities db = new DataEntities();
            int delai = DateTimeUnixTools.ConvertToUnixTimestamp((new DateTime()).AddMinutes(10));
            string nativeSQLQuery =
                " SELECT r.id_reservation , c.id_equipement, cl.nomClient, cl.prenomClient , r.dateDebut, r.dateFin " +
                "FROM client cl, concerne c, reservation r, equipements e, TypeEquipement te " +
                "WHERE c.id_equipement = e.id_equipement " +
                "AND e.id_TypeEquipement = te.id_TypeEquipement " +
                "AND c.id_reservation = r.id_reservation " +
                " AND e.id_equipement = " + idEq +
                " AND ( r.dateDebut BETWEEN " + debut + " AND " + (debut + 9) +
                " OR r.dateFin BETWEEN " + debut + " AND " + (debut + 9) +
                " OR " + (debut + 5) + " BETWEEN r.dateDebut AND r.dateFin" +
                //    " OR " + (debut+10) + " BETWEEN r.dateDebut AND r.dateFin" +
                " ) GROUP By r.id_reservation;";
            ObjectResult<EquipementResult> product = db.ExecuteStoreQuery<EquipementResult>(nativeSQLQuery);
            if (product.ToList().Count > 0)
                return false;
            else
                return true;
        }

        public static List<EquipementResult> whatHaveInDay()
        {
            int debut = DateTimeUnixTools.ConvertToUnixTimestamp (new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day,0,0,0));
            int fin = DateTimeUnixTools.ConvertToUnixTimestamp(new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 23, 59, 59));
            DataEntities db = new DataEntities();
            int delai = DateTimeUnixTools.ConvertToUnixTimestamp((new DateTime()).AddMinutes(10));
            string nativeSQLQuery =
                " SELECT r.id_reservation , c.id_equipement, cl.nomClient, cl.prenomClient , r.dateDebut, r.dateFin " +
                "FROM client cl, concerne c, reservation r, equipements e, TypeEquipement te " +
                "WHERE c.id_equipement = e.id_equipement " +
                "AND e.id_TypeEquipement = te.id_TypeEquipement " +
                "AND c.id_reservation = r.id_reservation " +
              //  " AND e.id_equipement = " + idEq +
                " AND ( r.dateDebut BETWEEN " + debut + " AND " + (fin) +
                " OR r.dateFin BETWEEN " + debut + " AND " + (fin) +
                " OR " + (debut) + " BETWEEN r.dateDebut AND r.dateFin " +
                " OR " + (fin) + " BETWEEN r.dateDebut AND r.dateFin " +
                //    " OR " + (debut+10) + " BETWEEN r.dateDebut AND r.dateFin" +
                " ) GROUP By r.id_reservation;";
            return  db.ExecuteStoreQuery<EquipementResult>(nativeSQLQuery).ToList();
            
        }

        
    }
}
